import _ from 'lodash';

export function propsSuffix(props, suffix) {
  if (!suffix) {
    return props;
  }

  return _.transform(
    props,
    (r, v, k) => {
      r[`${k}${suffix}`] = v;
    },
    {},
  );

  return returnValue;
}

export function pickProps(
  mappingProps,
  bindPropName = 'gdBind',
  suffix = '_GridData',
) {
  let withoutSuffixProps = _.transform(
    mappingProps,
    (r, propValue, propName) => {
      if (propName.endsWith(suffix)) {
        let withoutSuffix = propName.replace(suffix, '');
        if (false && attrs.hasOwnProperty(`v-model:${propName}`)) {
          r[`v-model:${withoutSuffix}`] = attrs[`v-model:${propName}`].value;
        } else {
          r[withoutSuffix] = propValue;
        }
      }
    },
    {},
  );

  let mergeProps = { ...mappingProps[bindPropName], ...withoutSuffixProps };

  let dynamicProps = {};
  Object.keys(mergeProps).forEach(key => {
    if (key.startsWith('v-model:')) {
      const p = key.replace('v-model:', '');
      dynamicProps[p] = mergeProps[key];
      dynamicProps[`onUpdate:${p}`] = e => emit(`update:${p}`, e);
    } else {
      if (false === mergeProps.hasOwnProperty(`v-model:${key}`)) {
        dynamicProps[key] = mergeProps[key];
      }
    }
  });

  return dynamicProps;
}

export function mappingEvent(emit, emits, suffix) {
  let bindEvent = emits.map(eName => {
    return [
      `on${_.upperFirst(eName)}`,
      e => {
        console.log('mappingEvent', `${eName}${suffix}`);
        emit(`${eName}${suffix}`, e);
      },
    ];
  });

  bindEvent = Object.fromEntries(bindEvent);
  return bindEvent;
}
